package com.winchain.chainbill.bill.biz.transfer.rx;

import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.po.CbXfe;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.BillTranEvent;
import com.winchain.chainbill.bill.biz.send.rx.BatchBillEvent;
import com.winchain.chainbill.bill.biz.transfer.service.BillXfeEventService;
import com.winchain.chainbill.esign.service.BatchSignService;
import com.winchain.chainbill.esign.vo.BatchSignVo;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillXfeSignSubscriber extends OnErrorNotImplementedSubscriber<BatchBillEvent<String, CbXfe>> {
	@Autowired
	private BatchSignService batchSignService;
	@Autowired
	private BillXfeEventService billXfeEventService;
	private static final String REASON = "������������";

	public void onApplicationEvent(BatchBillEvent<String, CbXfe> t) {
		String phoneCode = t.getStringValue("phoneCode");
		String location = t.getStringValue("location");
		List<String> signBillIds = new ArrayList();
		List<String> records = t.getRecs();
		for (String record : records) {
			BillTranEvent<CbXfe> event = this.billXfeEventService.getBillTranEvent(record, false);

			List<Bill<CbXfe>> children = (List<Bill<CbXfe>>) event.getChildren();
			if (children.size() > 0) {
				for (Bill<CbXfe> child : children) {
					if (((CbXfe) child.getSource()).getXfeId() != null) {
						signBillIds.add(child.getRec().getBillId());
					}
				}
			} else {
				signBillIds.add(record);
			}
		}
		BatchSignVo batchSignVo = new BatchSignVo();
		batchSignVo.setSignBillIds(signBillIds);
		batchSignVo.setBizType("11");
		batchSignVo.setPhoneCode(phoneCode);
		batchSignVo.setLocation(location);
		batchSignVo.setReason("������������");
		this.batchSignService.batchSign(batchSignVo);
	}
}
